package com.qbm.association.controller;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qbm.association.bean.BudgetDateSearch;
import com.qbm.association.bean.BudgetStatusEnum;
import com.qbm.association.bean.JoinClubPowerEnum;
import com.qbm.association.bean.ResultBean;
import com.qbm.association.config.Audience;
import com.qbm.association.entity.Budget;
import com.qbm.association.entity.Club;
import com.qbm.association.entity.JoinClub;
import com.qbm.association.entity.Student;
import com.qbm.association.mapper.BudgetMapper;
import com.qbm.association.service.BudgetService;
import com.qbm.association.service.StudentService;
import com.qbm.association.util.JWTTokenUtil;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Aron
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/association/budget")
public class BudgetController {

    @Resource
    private BudgetMapper budgetMapper;
    @Resource
    private BudgetService budgetService;
    @Resource
    private StudentService studentService;
    @Autowired
    private Audience audience;

    @GetMapping("getPageBudgetList")
    public ResultBean getPageBudgetList(BudgetDateSearch dateSearch, Integer moneyStatus, Integer clubId, Integer currentPage,String moneyDetail) {
        String begin = dateSearch.getBegin();
        String end = dateSearch.getEnd();
        //判断条件是否为空
        if (moneyStatus == 0) {
            moneyStatus = null;
        }

        Page<Budget> page = new Page<>(currentPage, 5);
        IPage<List<HashMap<Object, Object>>> page1 = budgetMapper.getAllBudget(
                page, clubId, begin, end, moneyStatus,moneyDetail);
        return ResultBean.success(page1, "获取成功");
    }

    @GetMapping("getBudgetTotal")
    public ResultBean getBudgetTotal(Integer clubId) {
        QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_id", clubId);
        List<Budget> budgetList = budgetMapper.selectList(queryWrapper);
        int totalBudget = 0;
        int incomeCount = 0;
        int expenditureCount = 0;

        for (int i = 0; i < budgetList.size(); i++) {
            if (BudgetStatusEnum.INCOME.getCode().equals(budgetList.get(i).getMoneyStatus())) {
                totalBudget += Integer.parseInt(budgetList.get(i).getMoney());
                incomeCount++;
            } else {
                totalBudget -= Integer.parseInt(budgetList.get(i).getMoney());
                expenditureCount++;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("totalBudget",totalBudget);
        map.put("incomeCount",incomeCount);
        map.put("expenditureCount",expenditureCount);
        return ResultBean.success(map, "获取成功");
    }

    @PostMapping("addBudget")
    public ResultBean addBudget(Budget budget, HttpServletRequest request) {
        int id = studentService.getByUsername(JWTTokenUtil.getUserName
                (request.getHeader(JWTTokenUtil.AUTH_HEAD_KEY), audience.getBase64Secret())).getId();
       budget.setUserId(id);
        boolean isTrue = budgetService.save(budget);
        Assert.isTrue(isTrue, "添加失败");
        return ResultBean.success("添加成功");
    }

    @PostMapping("deleteBudget")
    public ResultBean deleteBudget(Budget budget) {
        boolean isTrue = budgetService.removeById(budget);
        Assert.isTrue(isTrue,"删除失败");
        return ResultBean.success( "删除成功");
    }

}

